/*
 * Rhy Resizable element
 *
 * @author Tristan Maindron
 * @dependencies rhy.core.wrapper, rhy.core.config, rhy.core.view, rhy.extra.drag
 * 
 */

Rhy.extra.resize = {
    
    data: [],
    
    build: function(resizeDiv, config, cursor){  
        
        cursor.h.style.left   = ($r.vw.gp.getOffsetLeft(resizeDiv) + $r.vw.gp.getOffsetWidth(resizeDiv)) + 'px';
        cursor.v.style.top    = ($r.vw.gp.getOffsetTop(resizeDiv) + $r.vw.gp.getOffsetHeight(resizeDiv)) + 'px';
        
        cursor.h.style.cursor = 'e-resize';
        cursor.v.style.cursor = 's-resize';
        
        $r.ex.resize.adapt(resizeDiv, cursor.h, cursor.v);

        var xmin = $r.vw.gp.getOffsetLeft(resizeDiv) + config.x.min;
        var xmax = $r.vw.gp.getOffsetLeft(resizeDiv) + config.x.max;
        
        var ymin = $r.vw.gp.getOffsetTop(resizeDiv) + config.y.min;
        var ymax = $r.vw.gp.getOffsetTop(resizeDiv) + config.y.max;
        
        var resize = function(){
            var width  =  $r.vw.gp.getOffsetLeft(cursor.h) - $r.vw.gp.getOffsetLeft(resizeDiv);
            var height = $r.vw.gp.getOffsetTop(cursor.v)  - $r.vw.gp.getOffsetTop(resizeDiv);
            
            resizeDiv.style.padding = '0';
            
            if(config.x != false)
                resizeDiv.style.width  = width  + 'px';
            
            if(config.y != false)
                resizeDiv.style.height = height + 'px';
            
            $r.ex.resize.adapt(resizeDiv, cursor.h, cursor.v);
        };
        
        $r.ex.drag.apply(cursor.h, {
            axis: 'x',
            x : { min : xmin, max: xmax},
            onDrag: function(){resize(); config.onResize(resizeDiv, cursor) }
        });
        
        $r.ex.drag.apply(cursor.v, {
            axis: 'y',
            y : { min : ymin, max: ymax},
            onDrag: function(){resize(); config.onResize(resizeDiv, cursor) }
        });
        
        return cursor;
    },
    
    adapt: function(resizeDiv, hCursorDiv, vCursorDiv){
        vCursorDiv.style.width  = $r.vw.gp.getOffsetWidth(resizeDiv) + 'px';
        vCursorDiv.style.left   = $r.vw.gp.getOffsetLeft(resizeDiv) + 'px';
        hCursorDiv.style.height = $r.vw.gp.getOffsetHeight(resizeDiv) + 'px';
        hCursorDiv.style.top    = $r.vw.gp.getOffsetTop(resizeDiv) + 'px';
    },
    
    refresh: function(resizeDiv){
        var data = $r.ex.resize.data[resizeDiv.resizeId];
        $r.ex.resize.build(data.div, data.config, data.cursor);
        data.config.onResize(data.div, data.cursor);
    },
    
    apply: function(resizeDiv, config){
        config = $r.cfg.mgr.merge({
            onResize: function(){},
            onReady: function(){},
            x : { min : 20, max: 500},
            y : { min : 20, max: 500}
        }, config);
        
        var h = document.createElement('div');
        var v = document.createElement('div');
        var c = document.createElement('div');
        
        var content = resizeDiv.innerHTML;
        
        h.id = resizeDiv.id + "_hResizer";
        v.id = resizeDiv.id + "_vResizer";
        c.id = resizeDiv.id + "_content";
        
        for(var i in resizeDiv.childNodes){
            var node = resizeDiv.childNodes[i];
            try{
                resizeDiv.removeChild(node);
                c.appendChild(node);
            }
            catch(e){}
        }
        
        resizeDiv.appendChild(c);
        resizeDiv.appendChild(h);
        resizeDiv.appendChild(v);

        
        var cursor = {h: h, v: v};
        Rhy.extra.resize.build(resizeDiv, config, cursor);

        Rhy.extra.resize.data[Rhy.extra.resize.data.length] = {div: resizeDiv, cursor: cursor, config : config}; 
        
        config.onReady(resizeDiv, cursor);
        
        resizeDiv.resizeId = Rhy.extra.resize.data.length - 1;
    }
}
